extends Node
@onready var camera: Camera2D = $".."
@onready var state_chart: StateChart = $"../StateChart"

#==========================================================================
#zoom
func _on_zoom_control_state_physics_processing(delta: float) -> void:
	var dis = 0 - camera.position.y
	if dis>324:
		camera.zoom.x=clamp(324/dis,0.3,1.0)
		camera.zoom.y=clamp(324/dis,0.3,1.0)
	else:
		camera.zoom.x=1
		camera.zoom.y=1

#=============================================================================
#follow
var follow_len:float
func _on_follow_state_physics_processing(delta: float) -> void:
	follow_len=(camera.position-camera.follow.position).length()
	camera.position=camera.position.move_toward(camera.follow.position,delta*(250+250*smoothstep(100,200,follow_len)+2000*smoothstep(600,1000,follow_len)+6000*smoothstep(1000,1400,follow_len)))
	pass # Replace with function body.

#=================================================================================
#look at
var look_at_position:Vector2
var lookat_len:float
func _on_look_at_state_physics_processing(delta: float) -> void:
	lookat_len=(camera.position-look_at_position).length()
	camera.position=camera.position.move_toward(look_at_position,delta*(350+350*smoothstep(150,550,lookat_len)))

#==================================================================================
#shake
const _max_offset_len:float=5
const _offset_sub_strength:float=0.1
var _current_offset_len:float
func _on_shake_state_entered() -> void:
	_current_offset_len=_max_offset_len
func _on_shake_state_physics_processing(delta: float) -> void:
	if _current_offset_len<0:
		state_chart.send_event("shake_end")
	else:
		_current_offset_len-=_offset_sub_strength
		camera.offset = Vector2(randf_range(-_current_offset_len,_current_offset_len),randf_range(-_current_offset_len,_current_offset_len))
func _on_shake_state_exited() -> void:
	camera.offset=Vector2.ZERO


#============================================================================
#tilt
func _on_tilt_state_physics_processing(delta: float) -> void:
	pass # Replace with function body.

#=========================================================================================
#spring shake
var _spring_cnt:int=0
var ended:bool=true

func _on_spring_shake_state_entered() -> void:
	print("spring shake")
	_spring_cnt=0
	ended=false
func _on_spring_shake_state_physics_processing(delta: float) -> void:
	if ended:return
	if _spring_cnt>=8:
		ended=true
		state_chart.send_event("spring_shake_end")
	if _spring_cnt%2==0:
		camera.offset.x=move_toward(camera.offset.x,-50,delta*300)
		if camera.offset.x==-10:
			_spring_cnt+=1
	else:
		camera.offset.x=move_toward(camera.offset.x,50,delta*300)
		if camera.offset.x==10:
			_spring_cnt+=1
func _on_spring_shake_state_exited() -> void:
	print("spring skahe end")
	ended=true
	pass # Replace with function body.
